package com.wiscamp.ninechapters.problems.adapter.resources.tasks;

import com.wiscamp.ninechapters.problems.application.dto.commands.tasks.AssignProblemSetCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.tasks.CreateTaskByCollectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.tasks.CreateTaskByKnowledgePointsCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.tasks.CreateTaskByRuleCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.problems.ProblemVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.tasks.TaskVO;
import com.wiscamp.ninechapters.problems.application.services.TaskAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 练习任务服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")
@Tag(name = "题库-练习任务信息")
public class TaskController extends BaseController {

    private final TaskAppService taskAppService;

    public TaskController(TaskAppService taskAppService) {
        this.taskAppService = taskAppService;
    }

    /**
     * 分页获取练习任务列表
     *
     * @return 练习任务列表
     */
    @PostMapping("Tasks/Query")
    @Operation(summary = "分页获取练习任务列表")
    @HandleResourceException
    public RestResponse<Collection<TaskVO>> getTasksByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, taskAppService::getTasksByQuery);
    }

    /**
     * 通过任务编号获取习题集关联题列表
     *
     * @return 练习任务列表
     */
    @GetMapping("Tasks/{taskId}/Problems")
    @Operation(summary = "通过任务编号获取习题集关联题列表")
    @HandleResourceException
    public RestResponse<Collection<ProblemVO>> getProblemsByTask(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("taskId") long taskId) {
        var voList = taskAppService.getProblemsByTask(taskId);
        return ResourceUtils.getOkResponse(request, response, voList);
    }


    /**
     * 通过任务编号获取任务
     *
     * @param request
     * @param response
     * @param taskId
     * @return
     */
    @GetMapping("Tasks/{taskId}")
    @Operation(summary = "通过任务编号获取任务视图")
    @HandleResourceException
    public RestResponse<TaskVO> getTask(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                        @PathVariable("taskId") long taskId) {
        return ResourceUtils.getById(request, response, taskId, taskAppService::getTask);
    }

    /**
     * 通过用户编号获取未完成任务视图列表
     *
     * @param request
     * @param response
     * @param userId
     * @return
     */
    @GetMapping("Users/{userId}/Tasks/Uncompleted")
    @Operation(summary = "通过用户编号获取未完成任务视图列表")
    @HandleResourceException
    public RestResponse<Collection<TaskVO>> getUncompletedTask(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                               @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, taskAppService::getUncompletedTasks);
    }

    @GetMapping("Users/{userId}/Tasks/Completed/Pages/{pageNumber}")
    @Operation(summary = "通过用户编号分页获取已完成任务视图列表")
    @HandleResourceException
    public RestResponse<Collection<TaskVO>> getCompletedTask(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                             @PathVariable("userId") long userId,
                                                             @PathVariable("pageNumber") int pageNumber) {
        var tasks = taskAppService.getCompletedTaskByPage(userId, pageNumber);
        return ResourceUtils.getOkResponse(request, response, tasks);
    }


    @PostMapping("Tasks/Actions/Create/By/Collection")
    @Operation(summary = "通过题集代码获取创建新任务")
    @HandleResourceException
    public RestResponse<Long> createTaskByCollection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                     @RequestBody CreateTaskByCollectionCommand command) {
        var taskId = taskAppService.createTaskByCollection(command);
        return ResourceUtils.getOkResponse(request, response, taskId);
    }

    /*@GetMapping("users/{userId}/actions/create-task/byExaminationCode/{code}")
    @Operation(summary = "通过考试代码创建新练习任务")
    @HandleResourceException
    public RestResponse<Long> createTaskByExaminationCode(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("code") String code) {
        var taskId = taskAppService.useExaminationCode(userId, code);
        return ResourceUtils.getOkResponse(request, response, taskId);
    }*/

    /**
     * 通过知识点编号列表创建新任务
     *
     * @param request
     * @param response
     * @param pointIds
     * @return
     */
    @PostMapping("Tasks/Actions/Create/By/KnowledgePoints")
    @Operation(summary = "通过知识点编号列表创建新任务")
    @HandleResourceException
    public RestResponse<Long> createTaskByKnowledgePoints(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                          @RequestBody CreateTaskByKnowledgePointsCommand command) {
        var taskId = taskAppService.createTaskByKnowledgePoints(command);
        return ResourceUtils.getOkResponse(request, response, taskId);
    }


    @PostMapping("Tasks/Actions/Create/By/RegroupRule")
    @Operation(summary = "通过组题规则代码创建任务并返回任务编号")
    @HandleResourceException
    public RestResponse<Long> createTaskByRule(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody CreateTaskByRuleCommand command) {
        var taskId = taskAppService.createTaskByRule(command);
        return ResourceUtils.getOkResponse(request, response, taskId);
    }

    /*@GetMapping("users/{userId}/Tasks/actions/createByCourse/{courseId}/{contentId}")
    @Operation(summary = "通过课程及课程大纲编号获取练习任务")
    @HandleResourceException
    public RestResponse<Long> getTaskByCourseOutline(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("courseId") int courseId, @PathVariable("contentId") int contentId) {
        var taskId = taskAppService.createTaskByCourseOutline(courseId, contentId, userId);
        return ResourceUtils.getOkResponse(request, response, taskId);
    }*/

    @PostMapping("ProblemSets/Actions/Assign")
    @Operation(summary = "将习题集分配给若干用户创建练习任务")
    @HandleResourceException
    public RestResponse<Collection<Long>> assignProblemSet(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @RequestBody AssignProblemSetCommand command) {
        var taskIds = taskAppService.assignProblemSet(command);
        return ResourceUtils.getOkResponse(request, response, taskIds);
    }
}
